package com.cjw.service.impl.personal;

import cn.hutool.crypto.digest.MD5;
import com.cjw.config.annotation.AutoFieldSuffer;
import com.cjw.enums.AutoFieldType;
import com.cjw.enums.FileType;
import com.cjw.mapper.database.AdminDetailMapper;
import com.cjw.mapper.database.UserMapper;
import com.cjw.mapper.person.UserDetailMapper;
import com.cjw.pojo.assembler.UserAssembler;
import com.cjw.pojo.dto.database.UserDTO;
import com.cjw.pojo.dto.person.UserDetailDTO;
import com.cjw.pojo.entity.database.AdminDetail;
import com.cjw.pojo.entity.database.User;
import com.cjw.pojo.entity.person.UserDetail;
import com.cjw.pojo.query.person.UpdateBaseUser;
import com.cjw.pojo.query.person.UpdateSystemUser;
import com.cjw.service.database.MinioService;
import com.cjw.service.person.UserDetailService;
import com.cjw.util.BaseContextHolder;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * @create_time 2021.11.10
 * @author cjw
 * @descipe: 个人信息服务类
 */
@Service("userDetailService")
public class UserDetailServiceImpl implements UserDetailService {

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdminDetailMapper adminDetailMapper;

    @Autowired
    private MinioService minioService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @AutoFieldSuffer(AutoFieldType.ADD)
    public Boolean addUserDetail(UserDetail userDetail) {
        return userDetailMapper.insert(userDetail)==1;
    }

    @Override
    public UserDetailDTO findUserDetailByUserId() {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        return UserAssembler.toUserDetailDTO(userDetailMapper.queryUserDetailByUserId(user.getId()), user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateBaseUser(UpdateBaseUser updateBaseUser) {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        if (updateBaseUser.getSex().equals("1"))
            updateBaseUser.setSex("男");
        else if (updateBaseUser.getSex().equals("2"))
            updateBaseUser.setSex("女");
        else
            updateBaseUser.setSex(null);
        updateBaseUser.setUserId(user.getId());
        return userDetailMapper.updateUserDetailByUserId(updateBaseUser)==1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateSystemUser(UpdateSystemUser updateSystemUser) {
        if (!updateSystemUser.getNewPassword().equals(updateSystemUser.getConfirmPassword()))
            return false;
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        User user1 = new User();
        user1.setId(user.getId());
        User user2 = userMapper.selectOne(user1);
        String salt = user2.getSalt();
        String password = MD5.create().digestHex(updateSystemUser.getPassword()+salt);
        if (!user2.getPassword().equals(password))
            return false;
        salt = RandomStringUtils.randomAlphabetic(10);
        String newPassword = MD5.create().digestHex(updateSystemUser.getNewPassword()+salt);
        user2.setUserName(updateSystemUser.getUserName());
        user2.setSalt(salt);
        user2.setPassword(newPassword);
        return userMapper.updateByPrimaryKey(user2)==1;
    }

    @Override
    public Boolean updateIntroduce(String introduce) {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        return userDetailMapper.updateIntroduce(introduce, user.getId())==1;
    }

    @Override
    public String updateHeadImage(String imageUrl) {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        return userDetailMapper.updateImageUrl(imageUrl, user.getId()).toString();
    }

    @Override
    public String getUserHeadUrl() {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        if (user.getType() == 0) {
            UserDetail userDetail = new UserDetail();
            userDetail.setUserId(user.getId());
            return userDetailMapper.selectOne(userDetail).getHeadImage();
        } else if (user.getType() == 1) {
            AdminDetail adminDetail = new AdminDetail();
            adminDetail.setUserId(user.getId());
            return adminDetailMapper.selectOne(adminDetail).getHeadImage();
        }
        return null;
    }

    @Override
    public String uploadHeadImage(MultipartFile image) {
        return minioService.MinIOUpload(image, FileType.IMAGE.getCode());
    }

}
